Back
Templating
By Monkeymatt last edited on Saturday, May 20, 2006 at 1:11:02 pm by Monkeymatt. Page
3.
Here we will begin to make the system a little more complex with variables that need more explanation.
PHP Code
<?php
class Template{
// Define global variables
private $code=array(); // This will hold all of the code we get and all of the sections in a multidimensional array
private $file_root; // Holds the main directory where the template files are stored, defined in constructor
private $current_level=0; // Holds the current level in the level tracker (0 is the file, 1 and up are sections)
private $level_tracker=array(); // Holds a reference to the places in the main code array where we went through to get to where we are
private $current_item; // Holds a reference to the current place in the $code array
// Functions omitted here to shorten it up
?>
The newly added variables are the
$current_level, which is basically the index for the next variable:
$level_tracker. This is an array that holds a reference to the places in the
$code array which we stepped through to get where we are (which sections did we go through in the tree to get to where we are). For example, say we have a template like this:
Code
<{loop1}>
<{loop2}>
<{loop3}>
Loop3 Contents
<{/loop3}>
<{/loop2}>
<{/loop1}>
Say we then went into loop1, then loop2, and finally loop3 in that order.
$level_tracker[0] would contain a reference to the part of the
$code array that contained everything,
$level_tracker[1] would have a reference to the part with loop2 and down,
$level_tracker[2] would have everything with loop3 and down, and
$level_tracker[3] would have "Loop3 Contents".
The $current_level works in much the same way to provide quick access to the current section we are working on without needing to refer to a number of other variables.
Here we are going to add files to the $code array:
PHP Code
<?php
class Template{
// Omitted for shortness
// Adds files
public function add_file($file) {
if (!is_file($this->file_root.$file)) {
$this->error("add_file()", "File '$file' does not exist.");
}
$contents=file_get_contents($this->file_root.$file);
if (empty($contents)) {
$this->error("add_file()", "File '$file' is empty.");
} else {
$this->code['base']['****code****'].=$contents; // add $contents to the variable we initiated in the constructor
}
$this->level_tracker=array(); // clear of all other references because we started at the beginning again
$this->level_tracker[0]=&$this->code['base']; // Set bottom of level_tracker to be a reference to the base of the code array
$this->current_level=0;
$this->current_item=&$this->code['base']; // Current item is now the base of the code array, this variable can be used exactly the same as $level_tracker[0] right now
}
}
?>
This first checks to make sure we were given an actual file, then grabs the contents and checks if it has something in it. If it does, then it is added to the base of the $code array in the ****code**** section. Then we set the level_tracker variable to have references to the $code array, and set the current_item to have access to the exact same variable. (changing one would change the values all of them would see).
Next, we will demystify the large
$code variable.